import get_f from '@dfeidao/atom-nodejs/data/get-field';
import complex_query from '@dfeidao/atom-nodejs/db/complex-query';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { systemid } from '../atom/config';
interface Message {
	// cookie: {
	// 	uk: string;
	// 	[key: string]: string
	// };
	// urls: {
	// 	base: string;
	// 	origin: string;
	// 	url: string;
	// };
	// query: {};
	params: { teacher_no: string, productid: string };
	// headers: {};
	// captcha: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:app-home/s001,action_id:' + action_id);
	const query = complex_query(action_id, session_id, systemid);
	// 依据“教师编号从<班级教师>表中读取“班级编号”“班级名称”，根据"班级编号"查询<班级>获取“年级名称”

	const class_teacher = await query
		.add_field('class_teacher', 'class_id', 'class_id')
		.add_field('class_teacher', 'class_name', 'class_name')
		.where_eq('class_teacher', 'teacher_no', message.params.teacher_no)
		.where_eq('class_teacher', 'productid', message.params.productid)
		.exec();
	const class_id = get_f(class_teacher, 'class_id');
	const class_res = await query
		.add_field('class', 'grade', 'grade')
		.add_field('class', 'class_id', 'class_id')
		.add_field('class', 'class_name', 'class_name')
		.add_field('class', 'organization_type', 'organization_type')
		.where_in('class', 'class_id', class_id)
		.where_eq('class', 'productid', message.params.productid)
		.exec();
	log('Service end path:app-home/s001,action_id:' + action_id);
	return { data: class_res };
}
